package com.yan_jiu_sheng.LeetCodeHot100.AC;

import java.util.Arrays;
import java.util.HashMap;

/**
 * https://leetcode.cn/problems/longest-consecutive-sequence/description/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-05-24 15:01
 */
public class Test03 {
    public static void main(String[] args) {
        System.out.println(longestConsecutive(new int[]{0, 3, 7, 2, 5, 8, 4, 6, 0, 1}));
    }

    public static int longestConsecutive(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        Arrays.sort(nums);
        int res = 1;
        HashMap<Integer, Integer> hashMap = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            if (hashMap.isEmpty()) {
                hashMap.put(nums[i] + 1, 1);
                continue;
            }
            if (hashMap.containsKey(nums[i])) {
                int temp = hashMap.get(nums[i]) + 1;
                hashMap.put(nums[i] + 1, temp);
                res = Math.max(res, temp);
            } else {
                hashMap.put(nums[i] + 1, 1);
            }
        }
        return res;
    }
}
